package com.cqw.controller;


import com.cqw.domain.SysUser;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-05-06 09:56
 * @Description:
 */
@RestController
@RequestMapping
@Slf4j
@Api(tags = "restTemplate测试")
public class RestTemplateController {


    @Resource
    private RestTemplate restTemplate;


    /**
     * getForEntity 不带参数
     *
     * @return
     */
    @GetMapping("getForEntity")
    public String getForEntity() {
        ResponseEntity<String> forEntity = restTemplate.getForEntity("http://localhost:9999/getForEntity", String.class);
        System.out.println(forEntity.getBody());
        System.out.println(forEntity.getStatusCode()); //200 OK
        System.out.println(forEntity.getHeaders());
        System.out.println(forEntity.getStatusCodeValue()); //200
        return "OK";
    }


    /**
     * getForEntity 带参数
     *
     * @return
     */
    @GetMapping("getForEntityAndParam")
    public String getForEntityAndParam() {
        ResponseEntity<String> name = restTemplate.getForEntity("http://localhost:9999/getForEntityAndParam?name={1}", String.class, "曹青稳");
        System.out.println(name);
        return "OK";
    }


    /**
     * getForEntity RestFul带参数
     *
     * @return
     */
    @GetMapping("getForEntityAndParamUrl")
    public String getForEntityAndParamUrl() {
        ResponseEntity<String> name = restTemplate.getForEntity("http://localhost:9999/getForEntityAndParam/{1}", String.class, "曹青稳");
        System.out.println(name);
        return "OK";
    }


    /**
     * getForEntity map传参
     *
     * @return
     */
    @GetMapping("getForEntityAndParamMap")
    public String getForEntityAndParamMap() {
        String url = "http://localhost:9999/getForEntityAndParam?name={name}";
        HashMap<String, String> map = new HashMap<>();
        map.put("name", "周园园");
        ResponseEntity<String> name = restTemplate.getForEntity(url, String.class, map);
        System.out.println(name);
        return "OK";
    }


    /**
     * getForObject 不带参数
     *
     * @return
     */
    @GetMapping("getForObject")
    public String getForObject() {
        String forObject = restTemplate.getForObject("http://localhost:9999/getForEntity", String.class);
        System.out.println(forObject);
        return "OK";
    }

    //getForObject 跟 getForEntity一样，传参都一样，就是返回值不一样


    /**
     * postForEntity 传递key/value形式的参数
     *
     * @return
     */
    @GetMapping("postForEntity")
    public String postForEntity() {
        LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("name", "曹青稳");
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity("http://localhost:9999/postForEntity", map, String.class);
        System.out.println(stringResponseEntity.getBody());
        System.out.println(stringResponseEntity.getStatusCode());
        System.out.println(stringResponseEntity.getHeaders());
        System.out.println(stringResponseEntity.getStatusCodeValue());
        return "成功";
    }


    /**
     * postForEntity 带对象
     *
     * @return
     */
    @GetMapping("postForEntityAndObj")
    public String postForEntityAndObj() {
        try {
            //User.UserBuilder user = User.builder().userId(1L).userName("曹青稳").age(12).birthday(new Date());
            SysUser user = new SysUser();
            user.setUserId(1l);
            user.setUserName("曹青稳");
            ResponseEntity<SysUser> responseEntity = restTemplate.postForEntity("http://localhost:9999/postForEntityAndObj", user, SysUser.class);
            System.out.println(responseEntity.getBody());
            System.out.println(responseEntity.getStatusCode());
            System.out.println(responseEntity.getHeaders());
            System.out.println(responseEntity.getStatusCodeValue());
        } catch (RestClientException e) {
            e.printStackTrace();
            //System.out.println(e);
        }
        return "OK";
    }


    //postForObject  跟postForEntity一样，就是返回值的差异


    /**
     * 设置请求头
     */
    @GetMapping("testHeader")
    public String testHeader() {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.add("cookie","userName='曹青稳'");
        SysUser user = new SysUser();
        user.setUserId(1L);
        user.setUserName("曹青稳");
        HttpEntity<SysUser> userHttpEntity = new HttpEntity<>(user, httpHeaders);

        ResponseEntity<SysUser> responseEntity = restTemplate.exchange("http://localhost:9999/postForEntityAndObj", HttpMethod.POST, userHttpEntity, SysUser.class);
        System.out.println(responseEntity.getBody());
        System.out.println(responseEntity.getStatusCode());
        System.out.println(responseEntity.getStatusCodeValue());
        System.out.println(responseEntity.getHeaders());


        return "OK";
    }


}
